﻿using AutoMapper;
using Common.Damin;
using Common.Infrastructrue.Interface;
using Common.Service;
using MediatR;
using RabitPart.WritedPartApi.Application.Comman.RBACfile;

namespace RabitPart.WritedPartApi.Application.CommanHandel.RBACfile
{
    public class RBACUserDelecteCommanHandel : IRequestHandler<RBACUserDelecteComman, ApiResult>
    {
        private readonly IUserResponstity userRE;
        private readonly IRel_user_roleResponstity userroleRE;
        private readonly IMapper _mapper;
        private readonly ILogger<RBACUserUpdateCommanHandel> logger;
        public RBACUserDelecteCommanHandel(IUserResponstity userRE, IMapper mapper, ILogger<RBACUserUpdateCommanHandel> logger, IRel_user_roleResponstity userroleRE)
        {
            this.userRE = userRE;
            _mapper = mapper;
            this.userroleRE = userroleRE;
            this.logger = logger;
        }


        public async Task<ApiResult> Handle(RBACUserDelecteComman request, CancellationToken cancellationToken)
        { 

            try
            {
                var delecteUser = _mapper.Map<User>(request);
                delecteUser.DeleteDate = DateTime.Now;
                delecteUser.CreateOperatorName = "删除操作";
                delecteUser.IsDeleted = true;
                await userRE.UpdateAsync(delecteUser);
                return await Task.FromResult(ApiResult.Success(StatusCodeEnum.Ok));

            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除用户信息失败");
                return await Task.FromResult(ApiResult.Fail("删除用户信息失败", StatusCodeEnum.Fail));
            }
        }

    }
}
